package cn.security.auth.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;

/**
 * 返回Result
 * @param <T>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -7598829391091941077L;
    public static Integer SUCCESS_CODE = 1;
    public static Integer ERROR_CODE = 0;
    public static Integer UNEXPECTED_CODE = -1;
    private String message;
    @NotNull
    private Integer code;
    private T data;

    public static <T> Result<T> success(T data) {
        return customize(SUCCESS_CODE, data, (String)null);
    }

    public static <T> Result<T> success() {
        return customize(SUCCESS_CODE, null, (String)null);
    }

    public static <T>Result<T> success(String message){return customize(SUCCESS_CODE,message);}

    public static <T> Result<T> error(String message) {
        return error(ERROR_CODE, message);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return customize(code, null, message);
    }

    public static <T> Result<T> customize(Integer code, T data, String message) {
        Result<T> result = new Result();
        result.setMessage(message);
        result.setCode(code);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> customize(Integer code, T data) {
        Result<T> result = new Result();
        result.setCode(code);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> customize(Integer code,String message) {
        Result<T> result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> customize(Integer code) {
        Result<T> result = new Result();
        result.setCode(code);
        return result;
    }

    public boolean izSuccess() {
        return Objects.equals(this.code, SUCCESS_CODE);
    }

    public Result() {
    }

    public String getMessage() {
        return this.message;
    }

    public Integer getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$message = this.getMessage();
                Object other$message = other.getMessage();
                if (this$message == null) {
                    if (other$message != null) {
                        return false;
                    }
                } else if (!this$message.equals(other$message)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }


    public String toString() {
        return "Result(message=" + this.getMessage() + ", code=" + this.getCode() + ", data=" + this.getData() + ")";
    }
}
